package com.han.hotelplat.controller.employee;

import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.allinpay.xmltrans.pojo.TranxCon;
import com.allinpay.xmltrans.service.TranxServiceImpl;
import com.han.hotelplat.cache.redis.IRedisCache;
import com.han.hotelplat.comm.Constant;
import com.han.hotelplat.comm.ResponseWrapTo;
import com.han.hotelplat.comm.ResponseWrapTo.CodeType;
import com.han.hotelplat.comm.utils.AreaHelp;
import com.han.hotelplat.comm.utils.DateUtil;
import com.han.hotelplat.comm.utils.LogUtil;
import com.han.hotelplat.comm.utils.MD5;
import com.han.hotelplat.comm.utils.MsgHelp;
import com.han.hotelplat.comm.utils.NumberUtil;
import com.han.hotelplat.comm.utils.RequestUtil;
import com.han.hotelplat.comm.utils.StrUtil;
import com.han.hotelplat.comm.utils.UUIDUtils;
import com.han.hotelplat.pojo.cashextract.CashExtract;
import com.han.hotelplat.pojo.clientversion.ClientVersion;
import com.han.hotelplat.pojo.employee.Employee;
import com.han.hotelplat.pojo.hotelmanager.Hotelmanager;
import com.han.hotelplat.pojo.question.Question;
import com.han.hotelplat.pojo.wallet.Wallet;
import com.han.hotelplat.service.bill.BillService;
import com.han.hotelplat.service.cashextract.CashExtractService;
import com.han.hotelplat.service.clientversion.ClientversionService;
import com.han.hotelplat.service.employee.EmployeeService;
import com.han.hotelplat.service.hotelmanager.HotelmanagerService;
import com.han.hotelplat.service.question.QuestionService;
import com.han.hotelplat.service.wallet.WalletService;

@Controller
@RequestMapping("/employee/*")
public class EmployeeController {
	@Autowired
	private EmployeeService employeeService;
	
	@Autowired
	private IRedisCache redisCache;
	@Autowired
	private WalletService walletService;	
	
	@Autowired
	private ClientversionService clientversionService;
	
	@Autowired
	private CashExtractService cashExtractService;
	
	@Autowired
	private QuestionService questionService;
	
	@Autowired
	private BillService billService;
	
	@ResponseBody
	@RequestMapping(value = "changeTelAuthVc", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String changeTelAuthVc(String vcode){
		Employee em =  (Employee) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		if(em == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		
		String tel = (String) RequestUtil.getSessionAttribute("changetel");
		if(tel == null){
			return new ResponseWrapTo(-3, "验证码失效请重新获取", null).toJson();
		}
		
		if(tel.equals(em.getTel())){
			return new ResponseWrapTo(-1, "你当前已绑定该手机号", null).toJson();
		}
		
		Employee oldem = employeeService.findEmployeeByTel(tel);
		if(oldem != null){
			return new ResponseWrapTo(-2, "该手机号已被其他账号绑定，请先解绑该账号，在进行绑定", null).toJson();
		}
		
		String cashVcode = redisCache.get(Constant.CACHE_PHONE_VCODE_CHANGETEL+tel);
		if(!cashVcode.equals(vcode)){
			return new ResponseWrapTo(-3, "验证码错误", null).toJson();
		}
		//重置手机号码
		em.setTel(tel);
		employeeService.update(em);
		//清除验证码
		redisCache.delete(Constant.CACHE_PHONE_VCODE_CHANGETEL+tel);
		
		RequestUtil.setSessionAttribute(Constant.SESSION_LOGIN_USER, em);
		return new ResponseWrapTo(0, "手机号码替换成功", null).toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = "changeTelSendSMS", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String changeTelSendSMS(String tel){
		Employee em =  (Employee) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		if(em == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		if(tel.equals(em.getTel())){
			return new ResponseWrapTo(-1, "你当前已绑定该手机号", null).toJson();
		}
		Employee oldem = employeeService.findEmployeeByTel(tel);
		if(oldem != null){
			return new ResponseWrapTo(-2, "该手机号已被其他账号绑定，请先解绑该账号，在进行绑定", null).toJson();
		}
		
		int vcode = NumberUtil.getRandomNumber(1000, 9999);
		redisCache.set(Constant.CACHE_PHONE_VCODE_CHANGETEL+tel, vcode);
		RequestUtil.setSessionAttribute("changetel", tel);
		try {
			MsgHelp.sendCheckCode(tel, vcode+"",Constant.USER_TYPE_EMPLOYEE);
		} catch (ClientException e) {
			e.printStackTrace();
			return new ResponseWrapTo(CodeType.fail,"验证码发送异常",null).toJson();
		}
		return new ResponseWrapTo(CodeType.success,"验证码发送成功",null).toJson();
	}
	
	/**
	 * 
		 * @description	客服中心意见反馈
		 * @author  han1088
		 * @param q
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "submitQuestion", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String submitQuestion(Question q){
		Employee em =  (Employee) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		if(em == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		q.setUsertype(Constant.USER_TYPE_EMPLOYEE);
		q.setCreattime(new Date());
		q.setEndtime(new Date());
		q.setUserid(em.getUserid());
		q.setState(1);//待解决
		questionService.save(q);
		return new ResponseWrapTo(CodeType.success, "反馈已收到，请耐心等待回复", null).toJson();
	}
	
	/**
	 * 
		 * @description	
		 * @author  han1088
		 * @param token
		 * @param type 1IOS, 2Android
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "initApp", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String initApp(String token, Integer type,String pushkey, Integer osType) {
		Map<String, Object> data = new HashMap<String, Object>();
		LogUtil.info("帮工初始化token="+token+",pushkey:"+pushkey);
		if(token == null || token.isEmpty()){
			ClientVersion c = clientversionService.getNowVersion(Constant.APP_TYPE_EM, type);//查询酒店端最新版本
			data.put("appversion", c);
			data.put("user", null);
			return new ResponseWrapTo(0,"请使用手机号码登陆",data).toJson();
		}
		
		String userid = token.split("_")[0];
		String cacheToken = redisCache.get(Constant.SESSION_LOGIN_USER_TOKEN+userid);
		ClientVersion c = clientversionService.getNowVersion(Constant.APP_TYPE_EM, type);//查询酒店端最新版本
		data.put("appversion", c);
		if(cacheToken == null || !cacheToken.equals(token)){
			data.put("user", null);
			LogUtil.info("初始化登陆失败userid="+userid 
					+";cacheToken="+cacheToken
					+";token="+token);
			return new ResponseWrapTo(0,"请使用手机号码登陆",data).toJson();
		}
		Map result = employeeService.quickLogin(userid);
		int code = (int) result.get("code");
		Employee employee = null;
		if(code == 0){//登陆成功
			employee = (Employee) result.get("user");
			if(employee.getArea() != null){
				employee.setAreaModel(AreaHelp.getInstance().getAreaModels(employee.getArea()));
			}
			employee.setToken(token);
			employee.setPushkey(pushkey);
			employee.setClientype(osType);
			employee.setLastlogintime(new Date());
			employeeService.update(employee);
			RequestUtil.setSessionAttribute(Constant.SESSION_LOGIN_USER, employee);
			data.put("user", employee);
			System.out.println("登陆成功userid="+employee.getUserid());
			return new ResponseWrapTo(CodeType.success,"登陆成功",data).toJson();
		}
		return new ResponseWrapTo(0,"请使用手机号码登陆",null).toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = "getBankCard", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String getBankCard(){
		Employee em =  (Employee) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		if(em == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		if(StrUtil.isNullOrEmpty(em.getBankcard()) || StrUtil.isNullOrEmpty(em.getBankcard())){
			return new ResponseWrapTo(0, "还未绑定过银行卡", "[]").toJson();
		}
		return new ResponseWrapTo(CodeType.success, "数据获取成功", JSONObject.parseObject(em.getBankcard())).toJson();
	}
	
	/**
	 * 
		 * @description	提交提现申请
		 * @author  han1088
		 * @param money
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "submitExCash", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String submitExCash(String money, String paypassword, String vcode, String cardno){
		Employee em =  (Employee) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		em = employeeService.getEmployee(em.getUserid());
		if(em == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		Wallet w = walletService.getWallet(em.getUserid());
		float balance = w.getBalance();//当前余额
		//获取当月能提现的额度
		float maxcash = cashExtractService.getMaxCash(em.getUserid());
		if(Float.parseFloat(money)<=1){
			return new ResponseWrapTo(-1, "提现金额必须大于¥1.00。", null).toJson();
		}
		if(Float.parseFloat(money)>balance){
			return new ResponseWrapTo(-1, "输入金额超过账户余额。", null).toJson();
		}
		if(Float.parseFloat(money)>maxcash){
			return new ResponseWrapTo(-2, "输入金额超过本月剩余可提额度"+maxcash, null).toJson();
		}
		if(Float.parseFloat(money)>Constant.MAX_CASHMONEY_NOSMS && StrUtil.isNullOrEmpty(vcode)){
			return new ResponseWrapTo(-2, "需要使用手机验证码验证", null).toJson();
		}
		if(!Constant.CashExtract){
			return new ResponseWrapTo(-2, "不开放提现功能", null).toJson();
		}
		List<CashExtract> cex = cashExtractService.getTodayExCash(em.getUserid(),Constant.CE_STATE_CREAT);
		/*if(cex.size() >= 2){
			return new ResponseWrapTo(-3, "您今天已有2次提现申请不能再次申请了", null).toJson();
		}*/
		
		String mypaypassword = w.getPaypassword();
		if(mypaypassword == null){
			return new ResponseWrapTo(-4, "支付密码还未设置请到用户中心设置支付密码", null).toJson();
		}
		
		if(!mypaypassword.equals(MD5.toMD5(paypassword))){
			return new ResponseWrapTo(-5, "支付密码错误", null).toJson();
		}
		
		if(Float.parseFloat(money)>Constant.MAX_CASHMONEY_NOSMS){
			if(vcode == null || vcode.length() == 0){
				return new ResponseWrapTo(-2, "您当前的提现额度需要使用短信验证才能进行提现", null).toJson();
			}
			String cashVcode = redisCache.get(Constant.CACHE_PHONE_VCODE_EXCASH+em.getTel());
			if(!cashVcode.equals(vcode)){
				return new ResponseWrapTo(-3, "验证码错误", null).toJson();
			}
			redisCache.delete(Constant.CACHE_PHONE_VCODE_EXCASH+em.getTel());
		}
		CashExtract cashExtract = new CashExtract();
		String serial =  "w"+DateUtil.format(new Date(),"yyyyMMddHHmmss")
				+NumberUtil.getRandomNumber(100, 999);
		cashExtract.setSerial(serial);
		cashExtract.setCreattime(new Date());
		cashExtract.setEdittime(new Date());
		//需要扣除手续费1元
		cashExtract.setMoney(Float.parseFloat(money)-1);
		cashExtract.setState(Constant.CE_STATE_CREAT);
		cashExtract.setUserid(em.getUserid());
		String bankCard = em.getBankcard();
		if(StrUtil.isNullOrEmpty(bankCard)){
			return new ResponseWrapTo(-1, "不能提现，请先绑定银行卡", null).toJson();
		}
		String bankcardno = JSONObject.parseObject(bankCard).getString("bankcardno");
		if(!bankcardno.equals(cardno)){
			return new ResponseWrapTo(-1, "不能提现，您并未绑定该银行卡", null).toJson();
		}
		cashExtract.setBankcard(em.getBankcard());
		cashExtractService.save(cashExtract);
		//修改余额
		/*w.setBalance(w.getBalance()-Float.parseFloat(money));
		walletService.update(w);*/
		String taskId = cashExtract.getId().toString()+"="+cashExtract.getState();//提现申请的ID=状态
		//记录账单并修改余额
		billService.creatBill(w.getId(), null, Float.parseFloat(money), 
				Constant.BILL_TYPE_CASH,"帮工提现;", taskId, null, Constant.CHARGE_CHANNEL_TRANSFER,
				em.getRealname());
		return new ResponseWrapTo(0, "提现完成，正在等待银行处理，预计2个工作日内能够到账，请耐心等待。", serial).toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = "getVcodeExCash", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String getVcodeExCash(){
		Employee em =  (Employee) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		if(em == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		int vcode = NumberUtil.getRandomNumber(1000, 9999);
		redisCache.set(Constant.CACHE_PHONE_VCODE_EXCASH+em.getTel(), vcode);
		try {
			MsgHelp.sendCheckCode(em.getTel(), vcode+"",Constant.USER_TYPE_EMPLOYEE);
		} catch (ClientException e) {
			e.printStackTrace();
			return new ResponseWrapTo(CodeType.fail,"验证码发送异常",null).toJson();
		}
		return new ResponseWrapTo(0, "验证码发送成功", null).toJson();
	}
	
	/**
	 * 
		 * @description	校验金额是否可以提现
		 * @author  han1088
		 * @param money
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "authExCash", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String authExCash(String money){
		Employee em =  (Employee) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		if(em == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		Wallet w = walletService.getWallet(em.getUserid());
		float balance = w.getBalance();//当前余额
		//获取当月能提现的额度
		float maxcash = cashExtractService.getMaxCash(em.getUserid());
		if(Float.parseFloat(money)<1){
			return new ResponseWrapTo(-1, "提现金额必须大于¥1.00。", null).toJson();
		}
		if(Float.parseFloat(money)>balance){
			return new ResponseWrapTo(-1, "输入金额超过账户余额。", null).toJson();
		}
		if(Float.parseFloat(money)>maxcash){
			return new ResponseWrapTo(-2, "输入金额超过本月剩余可提额度"+maxcash, null).toJson();
		}
		if(Float.parseFloat(money)>Constant.MAX_CASHMONEY_NOSMS){
			return new ResponseWrapTo(-2, "需要使用手机验证码验证", null).toJson();
		}
		if(!Constant.CashExtract){
			return new ResponseWrapTo(-2, "不开放提现功能", null).toJson();
		}
		List<CashExtract> cex = cashExtractService.getTodayExCash(em.getUserid(),Constant.CE_STATE_CREAT);
		/*if(cex.size() >= 2){
			return new ResponseWrapTo(-3, "您今天已有2次提现申请不能再次申请了", null).toJson();
		}*/
		return new ResponseWrapTo(0, "通过验证", null).toJson();
	}
	
	/**
	 * 
		 * @description	获取余额以及当月可提现额度
		 * @author  han1088
		 * @param money
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getMyBalance", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String getMyBalance(){
		Employee em =  (Employee) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		if(em == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		Wallet w = walletService.getWallet(em.getUserid());
		
		//获取当月能提现的额度
		float maxcash = cashExtractService.getMaxCash(em.getUserid());
		Date arrivetime = DateUtil.addDay(new Date(), 1);
		Map<String, Object> data = new HashMap<String, Object>();
		float balance = w.getBalance();//当前余额
		if(StrUtil.isNullOrEmpty(w.getPaypassword())){
			data.put("ispaypassword", 1);//未设置支付密码
		}else{
			data.put("ispaypassword", 2);
		}
		data.put("balance", balance);
		data.put("maxcash", maxcash);
		data.put("arrivetime", "两个工作日到账");
		data.put("bankcard", JSONObject.parseObject(em.getBankcard()));
		data.put("sendsmsmoney", Constant.MAX_CASHMONEY_NOSMS);
		data.put("tips", "每笔提现固定手续费¥1.00");
		data.put("cashtips", "本月累计已提现：¥"+(Constant.MAX_EX_CASH - maxcash));
		return new ResponseWrapTo(CodeType.success, "数据获取成功", data).toJson();
	}
	
	/**
	 * 
		 * @description	
		 * @author  han1088
		 * @param vcode
		 * @param cardNu
		 * @param bankCode
		 * @param realName
		 * @param idcard
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "bindBankCard", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String bindBankCard(String vcode, String cardNu, 
			String bankCode, String realName, String idcard){
		Employee em =  (Employee) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		if(em == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		String cachedvcode = redisCache.get(Constant.CACHE_PHONE_VCODE_BINDBANKCARD+em.getTel());
		if(!vcode.equals(cachedvcode)){
			return new ResponseWrapTo(-1,"验证码错误",null).toJson();
		}
		
		String bindcount = em.getBindcount();
		JSONObject data = null;
		if(StrUtil.isNullOrEmpty(bindcount)){
			data = new JSONObject();
			data.put("date", DateUtil.formatYMD(new Date()));
			data.put("count", 0);
		}else{
			data = JSONObject.parseObject(bindcount);
		}
		String date = data.getString("date");
		int count = data.getIntValue("count");
		Date  changedate = DateUtil.parseYMD(date);
		if(DateUtil.daysDistance(changedate, new Date()) == 0){
			if(count < 2){
				count = count + 1;
				data.put("count", count);
				em.setBindcount(data.toJSONString());
				employeeService.update(em);
			}
			/*if(count>=2){
				return new ResponseWrapTo(-1,"已经超过当日可允许的最大次数2",null).toJson();
			}*/
		}
		boolean isfront=false;//是否发送至前置机（由前置机进行签名）如不特别说明，商户技术不要设置为true
		TranxServiceImpl tranxService=new TranxServiceImpl();
		try {
			Map<String,Object> re = tranxService.singleAcctVerify(TranxCon.tranURL, realName, 
					cardNu, idcard, bankCode, isfront,Constant.pfxPath,Constant.tltcerPath);
			int code = (int) re.get("code");
			if(code == 0){
				Map<String, String> bankCard = new HashMap<String, String>();
				bankCard.put("bankcardno", cardNu);
				String bankcardtype = Constant.bandMap.get(Integer.parseInt(bankCode));
				bankCard.put("bankcardtype", bankcardtype);
				bankCard.put("bankcode", bankCode);
				em.setBankcard(JSONObject.toJSONString(bankCard));
				employeeService.update(em);
				RequestUtil.setSessionAttribute(Constant.SESSION_LOGIN_USER,em);
			}
			String message = (String) re.get("message");
			redisCache.delete(Constant.CACHE_PHONE_VCODE_BINDBANKCARD+em.getTel());
			return new ResponseWrapTo(code,message,null).toJson();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResponseWrapTo(CodeType.fail,"银行卡绑定失败",null).toJson();
	}
	
	/**
	 * 
		 * @description	绑定银行卡接收验证码
		 * @author  han1088
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "bindBankCardSendSMS", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String bindBankCardSendSMS(){
		Employee em =  (Employee) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		if(em == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		String bindcount = em.getBindcount();
		JSONObject data = null;
		if(StrUtil.isNullOrEmpty(bindcount)){
			data = new JSONObject();
			data.put("date", DateUtil.formatYMD(new Date()));
			data.put("count", 0);
		}else{
			data = JSONObject.parseObject(bindcount);
		}
		String date = data.getString("date");
		int count = data.getIntValue("count");
		Date  changedate = DateUtil.parseYMD(date);
		if(DateUtil.daysDistance(changedate, new Date()) == 0){
			/*if(count>=2){
				return new ResponseWrapTo(-1,"已经超过当日可允许的最大次数2",null).toJson();
			}*/
		}
		if(DateUtil.daysDistance(changedate, new Date()) > 0){
		    data.put("date", DateUtil.formatYMD(new Date()));
			data.put("count", 0);
			em.setBindcount(data.toJSONString());
			employeeService.update(em);
		}
		RequestUtil.setSessionAttribute(Constant.SESSION_LOGIN_USER,em);
		
		int vcode = NumberUtil.getRandomNumber(1000, 9999);
		redisCache.set(Constant.CACHE_PHONE_VCODE_BINDBANKCARD+em.getTel(), vcode);
		try {
			MsgHelp.sendCheckCode(em.getTel(), vcode+"",Constant.USER_TYPE_EMPLOYEE);
		} catch (ClientException e) {
			e.printStackTrace();
			return new ResponseWrapTo(CodeType.fail,"验证码发送异常",null).toJson();
		}
		return new ResponseWrapTo(CodeType.success,"验证码发送成功",null).toJson();
	
		
	}
	
	/**
	 * 
		 * @description	查询卡号所属银行
		 * @author  han1088
		 * @param cardno
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "serchCardBin", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String serchCardBin(String cardno){
		Employee em =  (Employee) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		if(em == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		TranxServiceImpl tranxService=new TranxServiceImpl();
		boolean isfront=false;//是否发送至前置机（由前置机进行签名）如不特别说明，商户技术不要设置为true
		Map<String, Object> data = tranxService.serchCardBin(TranxCon.tranURL, cardno, isfront, Constant.pfxPath, Constant.tltcerPath);
		int code = (int) data.get("code");
		String message = (String) data.get("message");
		return new ResponseWrapTo(code, message, data).toJson();
	}
	/**
	 * 
		 * @description	用于手机号+验证码登陆
		 * @author  han1088
		 * @param phone 手机号
		 * @param code 验证码
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "setInfo", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String setInfo() {
		Employee em =  (Employee) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		Map<String, Object> data = new HashMap<String, Object>();
		if(em == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		String loginPassword = em.getPassword();
		Wallet wallet = walletService.getWallet(em.getUserid().toString());
		if(wallet.getState().intValue() == Constant.WALLET_STATE_NOTACTIVE){//还未激活过
			data.put("isSetPayPassword", 2);//未设置支付密码
		}else{
			data.put("isSetPayPassword", 1);//已设置支付密码
		}
		
		if(loginPassword == null){//还未激活过
			data.put("isSetLoginPassword", 2);//未设置支付密码
		}else{
			data.put("isSetLoginPassword", 1);//已设置支付密码
		}
		return new ResponseWrapTo(CodeType.success,"",data).toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = "quickLogin", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String quickLogin(HttpServletRequest req,String token,Integer clientype, String pushkey) {
		String userid = token.split("_")[0];
		String cacheToken = redisCache.get(Constant.SESSION_LOGIN_USER_TOKEN+userid).toString();
		if(!cacheToken.equals(token)){
			return new ResponseWrapTo(CodeType.fail,"登陆状态无效，请使用手机号码登陆",null).toJson();
		}
		Map result = employeeService.quickLogin(userid);
		int code = (int) result.get("code");
		Employee employee = null;
		if(code == 0){//登陆成功
			employee = (Employee) result.get("user");
			employee.setClientype(clientype);
			employee.setPushkey(pushkey);
			RequestUtil.setSessionAttribute(Constant.SESSION_LOGIN_USER, employee);
			employee.setLastlogintime(new Date());
			employeeService.update(employee);
			return new ResponseWrapTo(CodeType.success,"登陆成功",employee).toJson();
		}
		return new ResponseWrapTo(CodeType.fail,"登陆失败，请使用手机号码登陆",null).toJson();
	}
	
	/**
	 * 
		 * @description	用于手机号+验证码登陆
		 * @author  han1088
		 * @param phone 手机号
		 * @param code 验证码
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "login", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String login(HttpServletRequest req,String phone, String vcode, Integer clientype, String pushkey) {
		Map result = employeeService.login(phone, vcode);
		int code = (int) result.get("code");
		Employee employee = null;
		if(code == 0){//登陆成功
			employee = (Employee) result.get("user");
			employee.setClientype(clientype);
			employee.setPushkey(pushkey);
			String token = employee.getUserid()+"_"+MD5.toMD5(UUIDUtils.getUUID()+"_"+System.currentTimeMillis());
			redisCache.set(Constant.SESSION_LOGIN_USER_TOKEN+employee.getUserid(), token);
			RequestUtil.setSessionAttribute(Constant.SESSION_LOGIN_USER, employee);
			employee.setToken(token);
			employee.setLastlogintime(new Date());
			employeeService.update(employee);
			return new ResponseWrapTo(CodeType.success,"登陆成功",employee).toJson();
		}
		return new ResponseWrapTo(CodeType.fail,"验证码错误",null).toJson();
	}
	
	/**
	 * 
		 * @description	发送短信验证码
		 * @author  han1088
		 * @param phone 手机号
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "sendvcode", produces = "application/json;charset=UTF-8", method = { RequestMethod.POST,RequestMethod.GET })
	public String sendVcode(String phone) {
		int vcode = NumberUtil.getRandomNumber(1000, 9999);
		redisCache.set(Constant.CACHE_PHONE_VCODE_LOGIN_EM+phone, vcode);
		try {
			MsgHelp.sendCheckCode(phone, vcode+"",Constant.USER_TYPE_EMPLOYEE);
		} catch (ClientException e) {
			e.printStackTrace();
			return new ResponseWrapTo(CodeType.fail,"验证码发送异常",null).toJson();
		}
		return new ResponseWrapTo(CodeType.success,"验证码发送成功",null).toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = "updateEmployeeInfo", produces = "application/json;charset=UTF-8", method = { RequestMethod.POST,RequestMethod.GET })
	public String updateEmployeeInfo(String employee){
		Employee oldemployee = (Employee) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		if(oldemployee == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		Employee em = JSONObject.parseObject(employee, Employee.class);
		if(em.getId() == null){
			return new ResponseWrapTo(CodeType.fail, "资料不全不能更新", null).toJson();
		}
		em.setTel(oldemployee.getTel());
		em.setState(Constant.USER_STATE_CREAT);
		Map<String, String> bankCard = new HashMap<String, String>();
		bankCard.put("bankcardno", em.getBankcardno());
		bankCard.put("bankcardtype", em.getBankcardtype());
		bankCard.put("bankcode", em.getBankcode());
		em.setBankcard(JSONObject.toJSONString(bankCard));
		Map<String, Object> re = checkEmAttribute(em);
		int code = (int) re.get("code");
		String message = (String) re.get("message");
		if(code == -1){
			return new ResponseWrapTo(code, message, em).toJson();
		}
		employeeService.update(em);
		return new ResponseWrapTo(CodeType.success, "资料提交成功请等待审核", em).toJson();
	}
	
	public static Map<String, Object> checkEmAttribute(Employee em) {
		int code = -1;
		String message = "";
		Map<String, Object> data = new HashMap<String, Object>();
		if(em.getIcon() == null){
			message = "头像不能为空"; 
		}
		if(em.getRealname() == null){
			message = "姓名不能为空"; 
		}
		if(em.getSex() == null){
			message = "性别不能为空"; 
		}
		if(em.getWorkyears() == null){
			message = "工龄不能为空"; 
		}
		if(em.getLevel() == null){
			message = "清洁等级不能为空"; 
		}
		if(em.getIdcard() == null){
			message = "身份证号不能为空"; 
		}
		if(em.getBackpic() == null || em.getFacepic() == null){
			message = "身份证正反面不能为空"; 
		}
		if(StrUtil.isNullOrEmpty(em.getArea())){
			message = "所在地区不能为空"; 
		}
		if(StrUtil.isNullOrEmpty(em.getAddress())){
			message = "详细地址不能为空"; 
		}
		if(StrUtil.isNullOrEmpty(em.getEmergencyuser())){
			message = "紧急联系人不能为空"; 
		}
		if(StrUtil.isNullOrEmpty(em.getEmergencytel())){
			message = "紧急联系电话不能为空"; 
		}
		if(!StrUtil.isNullOrEmpty(message)){
			data.put("code", code);
			data.put("message", message);
			return data;
		}
		data.put("code", 0);
		data.put("message", "资料齐全");
		return data;
	}

	/**
	 * 
		 * @description	获取验证码
		 * @author  han1088
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getPasswordVC", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String getPasswordVC(){
		String phone = "";
		Employee em = (Employee) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		if(em == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		phone = em.getTel();
		//将用户钱包存进session
		RequestUtil.setSessionAttribute(Constant.SESSION_LOGIN_USER, em);
		int vcode = NumberUtil.getRandomNumber(1000, 9999);
		redisCache.set(Constant.CACHE_PHONE_VCODE_EM_PASSWORD+phone, vcode);
		try {
			MsgHelp.sendCheckCode(phone, vcode+"",Constant.USER_TYPE_EMPLOYEE);
		} catch (ClientException e) {
			e.printStackTrace();
			return new ResponseWrapTo(CodeType.fail,"验证码发送异常",null).toJson();
		}
		return new ResponseWrapTo(CodeType.success,"验证码发送成功",null).toJson();
		
	}

	/**
	 * 
		 * @description	验证码验证
		 * @author  han1088
		 * @param phone
		 * @param vcode
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "verifyVCode", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String verifyVCode(String vcode){
		String phone = "";
		Employee em = (Employee) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		if(em == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		phone = em.getTel();
		
		String cacheVCode = redisCache.get(Constant.CACHE_PHONE_VCODE_EM_PASSWORD+phone);
		if(cacheVCode == null){
			//返回值固定-3
			return new ResponseWrapTo(-3, "验证码失效请重新获取", null).toJson();
		}
			//返回值固定-3
		if(!cacheVCode.equals(vcode)){
			RequestUtil.setSessionAttribute("verifyVCode", Constant.VERIFYVCODE_NOPASS);
			return new ResponseWrapTo(-3, "验证码错误", null).toJson();
		}
		RequestUtil.setSessionAttribute("verifyVCode", Constant.VERIFYVCODE_PASS);
		redisCache.delete(Constant.CACHE_PHONE_VCODE_EM_PASSWORD+phone);
		return new ResponseWrapTo(CodeType.success, "验证成功", null).toJson();
	}

	/**
	 * 
		 * @description	设置密码
		 * @author  han1088
		 * @param password
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "setPassword", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String setPassword(String password){
		Integer result = (Integer) RequestUtil.getSessionAttribute("verifyVCode");
		Employee em = (Employee) RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER);
		//已经验证过
		if(result.intValue() == Constant.VERIFYVCODE_PASS.intValue()){
			em.setPassword(MD5.toMD5(password));
			employeeService.update(em);
			return new ResponseWrapTo(CodeType.success, "密码设置成功", null).toJson();
		}
		return new ResponseWrapTo(CodeType.fail, "验证码错误", null).toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = "authIdNum", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String authIdNum(String idNum){
		Employee em = RequestUtil.getSessionAttribute(Constant.SESSION_LOGIN_USER, Employee.class);
		if(em == null){
			return new ResponseWrapTo(CodeType.forbidden, "用户未登录", null).toJson();
		}
		if(em.getIdcard() == null){
			return new ResponseWrapTo(CodeType.fail, "未上传身份证号码", null).toJson();
		}
		if(em.getIdcard().equals(idNum)){
			return new ResponseWrapTo(CodeType.success, "身份证验证成功", null).toJson();
		}
		return new ResponseWrapTo(CodeType.fail, "身份证验证失败", null).toJson();
	}
	
	
	/**
	 * 
		 * @description	用于手机号+密码登陆
		 * @author  han1088
		 * @param phone 手机号
		 * @param code 验证码
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "loginByPassword", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String loginByPassword(HttpServletRequest req,String phone, String password) {
		Map result = employeeService.loginByPassword(phone, password);
		int code = (int) result.get("code");
		String message = (String) result.get("message");
		Employee employee = null;
		if(code == 0){//登陆成功
			employee = (Employee) result.get("user");
			String token = employee.getUserid()+"_"+MD5.toMD5(UUIDUtils.getUUID()+"_"+System.currentTimeMillis());
			redisCache.set(Constant.SESSION_LOGIN_USER_TOKEN+employee.getUserid(), token);
			RequestUtil.setSessionAttribute(Constant.SESSION_LOGIN_USER, employee);
			employee.setToken(token);
			employee.setLastlogintime(new Date());
			employeeService.update(employee);
			return new ResponseWrapTo(CodeType.success,message,employee).toJson();
		}
		return new ResponseWrapTo(CodeType.fail,message,null).toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = "logout", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET,RequestMethod.POST })
	public String logout(HttpServletRequest request){
		HttpSession session = RequestUtil.getSession();
		if (session != null) {
			Enumeration<String> keys = RequestUtil.getSession().getAttributeNames();
			while (keys.hasMoreElements()) {
				String key = (String) keys.nextElement();
				RequestUtil.getSession().removeAttribute(key);
			}
			Cookie[] cookies = request.getCookies();
			for (Cookie cookie : cookies) {
				cookie.setMaxAge(0);
			}
		}
		return new ResponseWrapTo(CodeType.success, "成功退出", null).toJson();
	}
}
